<template>
  <div>
    <el-dialog
      title="设置分类"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="分类" prop="errClassId">
            <el-select v-model="form.errClassId" placeholder="请选择分类">
              <el-option
                v-for="v in classifyData"
                :key="v.id"
                :label="v.name"
                :value="v.id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="备注">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="form.des"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogVisible: false,
      addOrEdit: false,
      form: {
        ids: [],
        errClassId: '',
        des: '',
      },
      formAdd: {
        subjectIds: [],
        myAnswer: '',
        errClassId: '',
        des: '',
      },
      rules: {
        errClassId: [
          { required: true, message: '请选择分类', trigger: 'blur' },
        ],
      },
      listQuery: {
        page: 1,
        limit: 9999,
        pid: 0,
        classType: '4', //1.试卷 2.考试 3.栏目 4.错题集
        status: '1',
        createBy: undefined
        // createBy: this.$store.getters.userId
      },
      classifyData: [],
    }
  },
  mounted() {
    this.$bus.$on('showDialogTestErrorClassify', this.init)
  },
  beforeDestroy() {
    this.$bus.$off('showDialogTestErrorClassify')
  },
  methods: {
    //显示弹窗
    init(row, myAnswer) {
      this.form = this.$options.data().form
      this.addOrEdit = row.constructor === Object
      if (this.addOrEdit) {
        this.formAdd.subjectIds = [row.id]
        this.formAdd.myAnswer = myAnswer
      } else {
        this.form.ids = row
      }
      this.getList()
    },
    async getList() {
      this.listQuery.createBy = this.$store.getters.userId
      let res = await this.$store.dispatch('sysClassifyList', this.listQuery)
      this.classifyData = res.data
      this.dialogVisible = true
    },
    submitForm(form) {
      this.formAdd.errClassId = this.form.errClassId
      this.formAdd.des = this.form.des
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          if (this.addOrEdit) {
            await this.$store.dispatch('addTestSubjectsError', this.formAdd)
            this.$bus.$emit('queryCollectStatus')
          } else {
            await this.$store.dispatch('updateTestSubjectsError', this.form)
            this.$bus.$emit('clearSelected')
          }
          this.done()
        }
      })
    },
    done() {
      this.dialogVisible = false
      this.successMsg('操作成功')
    },
  },
}
</script>